﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MethodWorx.CMS.UI.Models;
using MethodWorx.CMS.Content;
using MethodWorx.Core.DomainModel;
using MethodWorx.CMS.UI.Models.Shared;
using MethodWorx.CMS.Services;

namespace MethodWorx.CMS.UI.Controllers
{
    public class PageTemplateController : Controller
    {
        //
        // GET: /PageTemplate/
        private SchemaService service;

        public PageTemplateController(SchemaService schemaService)
        {
            this.service = schemaService;
        }

        [HttpGet]
        public ActionResult CreateRoot()
        {
            return View(new CreateRootPageTemplateModel(this.service, null));
        }

        [HttpPost]
        public ActionResult CreateRoot(string submit, PageTemplateBasicDetails pageTemplateBasicDetails)
        {
            switch(submit)
            {
                case "save":
                    if (this.ModelState.IsValid)
                    {
                        try
                        {
                            //
                            //  pass to the service
                            this.service.CreatePageTemplate(
                                pageTemplateBasicDetails.Name,
                                pageTemplateBasicDetails.Description,
                                true,
                                pageTemplateBasicDetails.Parent, 
                                false);
                        }
                        catch (BusinessRuleException bre)
                        {
                            foreach (var error in bre.Errors)
                            {
                                //  add the model error here
                                this.ModelState.AddModelError(error.Key, error.Message);
                            }
                        }
                        //
                        //  lets use the service
                        return RedirectToAction("Refresh", "Schema");
                    }
                break;
                case "cancel":
                    return RedirectToAction("Refresh", "Schema");
                break;
            }

            return View(new CreateRootPageTemplateModel(this.service, null, pageTemplateBasicDetails));
        }

        [HttpGet]
        public ActionResult Create(Guid id)
        {
        
            //
            //  ok, lets read the parameters in
            var model = new CreatePageTemplateModel(this.service, id);

            //  no page template, so
            if (model.PageTemplate == null)
                return RedirectToAction("Refresh", "Schema");
            else
                return View(model);
        }

        [HttpPost]
        public ActionResult Add(Guid id, string submit, string childChoice,
            Guid? existingPageTemplate,
            [Bind(Prefix = "ConstraintDetails")]ConstraintDetails constraintDetails)
        {
            var model = new CreatePageTemplateModel(this.service, id);

            if (model.PageTemplate == null)
                return RedirectToAction("Refresh", "Schema");
            else
            {
                if (ModelState.IsValid && existingPageTemplate.HasValue)
                {
                    //  get the child
                    var child = this.service.GetPageTemplateById(existingPageTemplate.Value);
                    if (child != null)
                    {
                        //
                        //  ok, we have the child
                        try
                        {                  
                            //
                            //  ok, we are creating a child here
                            //  so lets use the service
                            this.service.AddChildPageTemplateOf(model.PageTemplate,
                                child,
                                constraintDetails.MinimumRequired,
                                constraintDetails.MaximumAllowed,
                                constraintDetails.NameConstraint);

                            return RedirectToAction("Refresh", "Schema");
                        }
                        catch (BusinessRuleException bre)
                        {
                            foreach (var error in bre.Errors)
                            {
                                //  add the model error here
                                this.ModelState.AddModelError(error.Key, error.Message);
                            }
                        }
                    }
                }

                return View("CreatePageTemplate", model);
            }
        }

        [HttpPost]
        public ActionResult Create(Guid id, string submit, string childChoice,
            [Bind(Prefix = "PageTemplateBasicDetails")]PageTemplateBasicDetails pageTemplateDetails,
            [Bind(Prefix="ConstraintDetails")]ConstraintDetails constraintDetails)
        {
            //
            //  ok, lets read the parameters in
            var model = new CreatePageTemplateModel(this.service, id);

            //  no page template, so
            if (model.PageTemplate == null)
                return RedirectToAction("Refresh", "Schema");
            else
            {
                if (ModelState.IsValid)
                {
                    try
                    {                        
                        //
                        //  ok, we are creating a child here
                        //  so lets use the service
                        this.service.ChildChildPageTemplateOf(model.PageTemplate,
                            pageTemplateDetails.Name,
                            pageTemplateDetails.Description,
                            constraintDetails.MinimumRequired,
                            constraintDetails.MaximumAllowed,
                            constraintDetails.NameConstraint);

                        return RedirectToAction("Refresh", "Schema");
                    }
                    catch (BusinessRuleException bre)
                    {
                        foreach (var error in bre.Errors)
                        {
                            //  add the model error here
                            this.ModelState.AddModelError(error.Key, error.Message);
                        }
                    }

                    
                }

                return View(model);
            }
        }
    }
}
